//
//  SegmentView.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/9/22.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "SegmentView.h"

@interface SegmentView()
@property(nonatomic,strong)UIButton *selectBtn;
@end
@implementation SegmentView

-(instancetype)initWithFrame:(CGRect)frame withItems:(NSArray*)items {
    
    self = [super initWithFrame:frame];
    if (self){
        
        
//        UISegmentedControl
        self.selectBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        
        for (int i=0;i<items.count;i++) {
            UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame =CGRectMake((self.bounds.size.width/items.count)*i, 0,self.bounds.size.width/items.count, self.bounds.size.height);
            [btn setTitle:[items objectAtIndex:i] forState:UIControlStateNormal];
            btn.titleLabel.font =F(14);
           
            [btn addTarget:self action:@selector(gotoClick:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag=i;
            [self addSubview:btn];
            [btn setBackgroundColor:self.tintColor];
            [btn setTitleColor:self.textColor forState:UIControlStateNormal];
            if (btn.tag==0) {
                [self.selectBtn setBackgroundColor:self.selectColor];
                [self.selectBtn setTitleColor:self.textSelectColor forState:UIControlStateNormal];
                self.selectBtn=btn;
               
            }


        }
        
     
        
    }
    return self;
}
- (void)gotoClick:(UIButton*)sender
{
    self.selectBtn=sender;
    [sender setBackgroundColor:self.selectColor];
    [sender setTitleColor:self.textSelectColor forState:UIControlStateNormal];
    if (!self.selectBtn) {
        [sender setBackgroundColor:self.tintColor];
        [sender setTitleColor:self.textColor forState:UIControlStateNormal];
    }
}
@end
